package com.study.it.sms;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


public class SmsUtil {
    private String url;
    private String uid;
    private String key;

    public void setUid(String uid) {
        this.uid = uid;
    }
    public void setKey(String key) {
        this.key = key;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String sendSms(String mobile, String msg){
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(this.url);//"https://utf8api.smschinese.cn/
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
        NameValuePair[] data ={
                new NameValuePair("Uid", uid)
                ,new NameValuePair("Key", key)
                ,new NameValuePair("smsMob",mobile)
                ,new NameValuePair("smsText",msg)};
        post.setRequestBody(data);
        try {
            client.executeMethod(post);
            String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
            System.out.println(result);  //打印返回消息状态
            post.releaseConnection();
            return result;
        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }
}
